package com.abigdreamer.im.client.module.main.headtree;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import com.abigdreamer.im.client.resourse.ResourcesManagement;
import com.abigdreamer.swing.component.JCLabel;
import com.abigdreamer.util.ImageUtil;

public class NodeIcon extends JCLabel {

	private static final long serialVersionUID = 1L;
	
	private int isOnline = BufferedImage.TYPE_BYTE_GRAY;
	private Image icon;
	private String iconPath = "1.gif";
	private int size = 40;

	public NodeIcon() {
	}
	
	public NodeIcon(String iconNum, boolean isOnline) {
		this(iconNum, isOnline, 40);
		size = 40;
	}
	
	public NodeIcon(String iconNum, boolean isOnline, int size) {
		setNodeInfo(iconNum, isOnline, size);
	}
	
	public void setNodeInfo(String iconNum, boolean isOnline, int size) {
		this.size = size;
		this.iconPath = iconNum + ".gif";
		if (true == isOnline) {
			this.isOnline = BufferedImage.TYPE_INT_BGR;
		} else {
			this.isOnline = BufferedImage.TYPE_BYTE_GRAY;
		}
		
		paint();
	}

	public void paint() {
		icon = ResourcesManagement.getImageIcon("head/" + iconPath, size, size).getImage();
		BufferedImage bufferedImage = new BufferedImage(size, size, isOnline);
		Graphics gi = bufferedImage.getGraphics();
		gi.drawImage(icon, 0, 0, null);
		gi.dispose();
//		Graphics2D g2d = (Graphics2D) g;
//		g2d.drawImage(bufferedImage, null, 0, 0);
		
		setIcon(new ImageIcon(ImageUtil.makeRoundedCorner(bufferedImage, 8)));
	}

	/**
	 * 
	 * @param isOnline
	 */
	public void changeIconState(boolean isOnline) {
		if (isOnline) {
			this.isOnline = BufferedImage.TYPE_INT_BGR;// 在线
		} else {
			this.isOnline = BufferedImage.TYPE_BYTE_GRAY;// 非在线
		}
		this.paint();
		this.repaint();
	}
}
